package com.allen.base.widget.tab;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;

public class DefaultTabAnimation implements TabAnimation{

    private int size = 0;
    private Paint paint = new Paint();

    public DefaultTabAnimation(Context context) {
        final float scale = context.getResources().getDisplayMetrics().density;
        size = (int) (4F * scale + 0.5f);
    }

    private void drawCircle(Canvas canvas, int centerX, int centerY) {
        canvas.drawCircle(centerX, centerY, size / 2, paint);
    }

    @Override
    public void onDraw(XTabLayout view, Canvas canvas, IndicatorScrollData data) {

    }

    @Override
    public void onDraw(XTabLayout view, Canvas canvas, BitmapDrawable drawable, int centerX, int centerY, int width, int height, float offset) {
        paint.setColor(view.getTextSelectColor());

        int w = width;
        int h = height;
        int left, top, right, bom;
        if(offset > 0.9999 || offset <= 0) {
            left = centerX - w / 2;
            right = centerX + w /2;
            top = centerY - h / 2;
            bom = centerY + h / 2;
        } else if(offset < 0.2){
            double s = (0.2 - offset) / 0.2;
            w = (int)(s * w);
            h = (int)(s * h);
            if(w < size || h < size){
                drawCircle(canvas, centerX, centerY);
                return ;
            }
            left = centerX - w / 2;
            right = centerX + w /2;
            bom = centerY + h / 2;
            top = bom - h;

        } else if(offset > 0.8) {
            double s = (1F - (1F - offset) / 0.2);
            w = (int)(s * w);
            h = (int)(s * h);
            if(w < size || h < size){
                drawCircle(canvas, centerX, centerY);
                return ;
            }
            left = centerX - w / 2;
            right = centerX + w /2;
            bom = centerY + h / 2;
            top = bom - h;
        }
        else {
            drawCircle(canvas, centerX, centerY);
            return ;
        }

        drawable.setBounds(left, top, right, bom);
    }
}
